<?php
	require_once("scripts/EKRating.php");
	require_once("scripts/EKEvaluation.php");
	require_once("scripts/EKEvent.php");
  
	# $_GET["ratingType"]  1="A",2="B"
	# $_GET["ownerType"]   1="d", 2="p", 3="t"
 	# $_GET["ownerId"]     id of the document/paragraph/talk 
	# $_GET["uid"]         author id
	# $_GET["value"]       rating value

	header('Content-type: text/plain');
	$rType = array(1=>"A", 2=>"B");
	$oType = array(1=>"d", 2=>"p", 3=>"t");
  
	session_start();
  
 	if(!isset($_SESSION["userid"]) || $_SESSION["userid"] == "")
	// Exception 1: userid illegal
  	{
  		print "Please login to give rating!";
  	}else{
  		$node = EKRating::getSameRating($oType[$_GET["ownerType"]], $_SESSION["userid"], "_".$_GET["ownerId"], $rType[$_GET["ratingType"]]);
  		if($node)
		// Exception 2: Already finished rating
  		{
  			print "You have already finished rating!";	
  		}else{
  		// Normal condition
  			$thisRatingId = EKRating::insertNewRating(
								$oType[$_GET["ownerType"]], 
								"_".$_GET["ownerId"],
								$rType[$_GET["ratingType"]],
								$_GET["value"]
								);
			
			EKUser::insertRecord($_SESSION["userid"], "r", $thisRatingId);
			EKRating::updateRating($oType[$_GET["ownerType"]],"_".$_GET["ownerId"]);
			
			# Save rating create event
			$event = new EKEvent($_SESSION['userid'],"create","r",$thisRatingId);
			$event->save();
			
			print "Thank you for rating!";
		}
  	}
?>
